package com.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.org.mapper.CommunityAdminMapper;
import com.org.mapper.CommunityMapper;
import com.org.pojo.Community;
import com.org.common.PageBean;
import com.org.service.CommunityService;
import com.org.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CommunityServiceImpl
 * Package: com.org.service.impl
 * Description:
 *
 * @Author Origin
 * @Create 2024/2/2 10:11
 * @Version 1.0
 */

@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private CommunityAdminMapper communityAdminMapper;


    @Override
    public PageBean<Community> list(Integer pageNum,
                                    Integer pageSize,
                                    String province,
                                    String city,
                                    String districtOrCountry,
                                    String name,
                                    String state) {
        //1. 创建PageBean对象
        PageBean<Community> pb = new PageBean<>();

        //2. 开启分页查询 PageHelper
        PageHelper.startPage(pageNum, pageSize);

        QueryWrapper<Community> qw = new QueryWrapper<>();
        qw.like("province", province)
                .like("city", city)
                .like("district_or_country", districtOrCountry)
                .like("name", name)
                .like("state", state);

        List<Community> as = communityMapper.selectList(qw);

        //Page中提供了方法，可以获取PageHelper分页查询后 得到的总记录条数和当页数据
        Page<Community> p = (Page<Community>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());  //总共多少页
        pb.setItems(p.getResult()); //当前页内容
        return pb;
    }

    @Override
    public void add(Community community) {
        community.setCreateTime(LocalDateTime.now());
        community.setUpdateTime(LocalDateTime.now());
        community.setState("待审核");
        communityMapper.add(community);
        Integer i = communityMapper.getByCreateId(community.getCreateUserId()).getId();
        communityAdminMapper.updateCommunityId(i, community.getCreateUserId());
    }

    @Override
    public Community getById(Integer id) {
        Community c = communityMapper.getById(id);
        return c;
    }

    @Override
    public void update(Community community) {
        community.setUpdateTime(LocalDateTime.now());
//        System.out.println(community.getDescribe());
        communityMapper.update(community);
    }

    @Override
    public void updateState(String state, Integer communityId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        communityMapper.updateState(state, communityId);
    }

    @Override
    public void delete(Integer id) {
        communityMapper.deleteById(id);
    }
}
